<?php
namespace system\service;

use herosphp\core\Log;
use herosphp\utils\ArrayUtils;
use system\service\interfaces\IAreaService;
use common\service\CommonService;
use herosphp\core\Loader;

Loader::import('system.service.interfaces.IAreaService');

/**
 * system(Service)接口实现
 * @package system\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class AreaService extends CommonService implements IAreaService {

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having); // TODO: Change the autogenerated stub
        $pids = array();
        foreach ( $items as $value ) {
            $pids[] = $value['pid'];
        }

        $pitems = $this->getModelDao()->getItems(array('id' => array('$in' => $pids)));
        $pitems = ArrayUtils::changeArrayKey($pitems, 'id');
        foreach ( $items as $key => $val ) {
            if ( $pitems[$val['pid']] ) {
                $items[$key]['pname'] = $pitems[$val['pid']]['name'];
            } else {
                $items[$key]['pname'] = '顶级父类';
            }
        }
        return $items;
    }

    /**
     * 生成js文件
     * @return mixed
     */
    public function createJsFile() {

        $model = Loader::model('Area');
        $timer = timer();
        $province = $model->getItems(array('pid' => 0));
        $p = ''; //身份
        $c = ''; //城市
        $d = ''; //地区
        foreach ( $province as $value ) {
            if ( $p == '' ) {
                $p .= "'{$value['id']}':'{$value['name']}'";
            } else {
                $p .= ", '{$value['id']}':'{$value['name']}'";
            }

            $citys = $model->getItems(array('pid' => $value["id"]));
            if ( $c == '' ) {
                $c .= "'{$value['id']}' : [";
            } else {
                $c .= ", '{$value['id']}' : [";
            }
            $cc = array();
            foreach ($citys as $city) {
                $cc[] = "{'id':'{$city['id']}', 'name':'{$city['name']}'}";

                $regions = $model->getItems(array('pid' => $city["id"]));;
                if ( $d == '' ) {
                    $d .= "'{$city['id']}' : [";
                } else {
                    $d .= ", '{$city['id']}' : [";
                }
                $gg = array();
                foreach ($regions as $region) {
                    $gg[] = "{'id':'{$region['id']}', 'name':'{$region['name']}'}";
                }
                $d .= implode(", ", $gg)."]";
            }
            $c .= implode(", ", $cc)."]";

        }
        $buffer = "var __AREADATA__ = {\n";
        $buffer .= "\t'prov' : {";
        $buffer .= $p."}, \n";
        $buffer .= "\t'city' : {";
        $buffer .= $c."}, \n";
        $buffer .= "\t'dist' : {";
        $buffer .= $d."}, \n";
        $buffer .= "};";

        //Log::info(timer()-$timer);
        $tempContent = file_get_contents(self::JS_FILE_PATH.'JAreaData.tpl');
        $tempContent = str_replace("{code}", $buffer, $tempContent);
        $jsfile = self::JS_FILE_PATH.'JAreaData.js';
        return file_put_contents($jsfile, $tempContent);

    }
}
